<?php
namespace app\admin\controller;

use bbweb\services\UtilService as Util;
use think\facade\Config;
use app\admin\model\system\{SystemAdmin as AdminModel, SystemMenus, SystemRole};
use think\Request;

class Index extends AuthController
{
	public function index()
	{
        //获取当前登录后台的管理员信息
        $adminInfo = $this->adminInfo;
        $roleid = $adminInfo['roleid']??0;
        $this->assign([
            'menuList' => SystemMenus::menuList(),
            'site_name' => sys_config('site_name') ?? Config::get('hello.title'),
            'site_url' => sys_config('site_url') ?? Config::get('hello.site_url'),
            'new_order_audio_link' => sys_config('new_order_audio_link'),
            'role_name' => $roleid ? SystemRole::where('id', $roleid)->value('role_name') : '管理员',
            //'workermanPort' => Config::get('workerman.admin.port')
        ]);
        return $this->fetch();
	}

	public function main()
	{
	}

    /**
     * 待办事统计
     * @param int
     * @return false|string
     */
    public function Jnotice()
    {
        header('Content-type:text/json');
        $data = [];
        $data['ordernum'] = 0;//待发货

        $data['inventory'] = 0;//库存
        $data['commentnum'] = 0;//评论
        $data['reflectnum'] = 0;//提现
        $data['msgcount'] = 1;
        //新订单提醒
        $data['newOrderId'] = 0;
        return app('json')->success('ok', $data);
    }

    /*
     * 个人信息
     */
    public function info(Request $req)
    {
        $id = $this->adminId;
        if (!$id) $this->error('参数错误');
        $admin = AdminModel::get($id);
        if (!$admin) $this->error('数据不存在!');
        if($req->isPost()) {
            $data = Util::postMore([
                'conf_password',
                'password',
                'real_name',
                'phone'
            ]);
            if (!$data['real_name']) $this->error('请输入真实姓名');
            if (!empty($data['password']) && $data['password'] != $data['conf_password']) $this->error('两次输入密码不相同');

            if (!empty($data['password'])) {
                $login = true;
                $data['password'] = md5(md5($data['password']) . Config::get('hello.secret'));
            } else {
                $login = false;
                unset($data['password']);
            }
            unset($data['conf_password']);

            $data['update_at'] = time();
            $res = AdminModel::where('id', $id)->update($data);
            if($res) {
                if($login) {
                    AdminModel::clearLoginInfo();
                    $this->success('修改成功、请重新登陆！','',[],2);
                } else {
                    $this->success('修改成功！','',[],3);
                }
            } else {
                $this->error();
            }
        }
        $this->assign('admin', $admin);
        return $this->fetch();
    }
}
